<?php
//变量


class MyDB extends SQLite3
{

   public function __construct()
   {
      $this->open('./db/task.db');
   }

   public function insert($testJson,$tableName)
   {
      $de_json = json_decode($testJson, TRUE);
      // var_dump($de_json);

      // foreach ($testJson as $sqljson) {
         $sql = "insert into ".$tableName."(";
         foreach ($de_json as $key=>$value) {
            $sql = $sql . "'" . $key . "'" . ",";
         }
         $sql = substr($sql, 0, -1);
         $sql = $sql . ') values(';
         foreach ($de_json as $key => $value) {
            $sql = $sql . "'" . $value . "'" . ",";
         }
         $sql = substr($sql, 0, -1);
         $sql = $sql . ')';
         $ret=$this->exec($sql); 
         echo '{"sql":"'.$sql.'","result":"'.'0'.'"}';
      // }
   }
   public function select($sql)
   {
      $results = array();    
      $ret = $this->query($sql);
      while ($row = $ret->fetchArray(SQLITE3_ASSOC)) {
         $results[]=$row;
      }
      echo json_encode($results);
   }
   public function delete($data,$tableName)
   {

      $sql = "delete from ".$tableName." where ".$data["id_name"]."=" .$data["id"];
      $this->exec($sql);
      echo '{"sql":"'.$sql.'","result":"'.'0'.'"}';
   }
   public function update($data,$tableName)
   {

      $sql = "update ".$tableName." set ".$data["param_name"]."='".$data["param_value"]."' where ".$data["id_name"]."=" .$data["id"];
      $this->exec($sql);
      echo '{"sql":"'.$sql.'","result":"'.'0'.'"}';
   }
   public function top($data,$tableName)
   // public function top()
   {
      // $tableName="task";
      // $data=array("id_name"=>"task_id","id"=>"1","Elon"=>"43");
      $sql = "select max(".$tableName."_priority) as max_num from ".$tableName;
      $results = array(); 
      $ret = $this->query($sql);
      while ($row = $ret->fetchArray(SQLITE3_ASSOC)) {
         $results[]=$row;
      }
      $max_num=$results[0]["max_num"];
      $max_num=$max_num+1;
      $sql = "update ".$tableName." set ".$tableName."_priority=".$max_num." where ".$data["id_name"]."=".$data["id"];
      $ret = $this->exec($sql);
      echo '{"sql":"'.$sql.'","result":"'.'0'.'"}';
   }

   public function getCount($tableName)
   {
      $sql = "select count(*) as len from ".$tableName;
      $results = array(); 
      $ret = $this->query($sql);
      while ($row = $ret->fetchArray(SQLITE3_ASSOC)) {
         $results[]=$row;
      }
      echo '{"sql":"'.$sql.'","result":'.$results[0]["len"].'}';
   }
}
error_reporting(0);
$db = new MyDB();
if ($db) {
   $option = $_POST['option'];
   $data = $_POST['data'];
   $tableName = $_POST['tableName'];

   switch ($option) {
      case 'select':
         echo '{"task":';
         $db->select("select * from task,class where task.task_class=class.class_id and is_finished==0 order by task_priority desc");
         echo ',"class":';
         $db->select("select * from class order by class_priority desc");
         echo '}';
         break;
      case 'history':
         // $temp=json_decode($data, TRUE);
         $sql="select * from task,class where task.task_class=class.class_id and class.status=1 and task.is_finished>=0 order by task_id desc ".$data["limit"];
         $db->select($sql);
         // echo '{"sql":"'.$sql.'","result":"0"}';
         break;
      case 'class':
         $db->select("select * from class order by class_priority desc");
         break;
      case 'insert':
         // var_dump($data);
         $db->insert($data,$tableName);
         break;
      case 'delete':
         $db->delete(json_decode($data, TRUE),$tableName);
         break;
      case 'update':
         $db->update(json_decode($data, TRUE),$tableName);
         break;
      case 'top':
         $db->top(json_decode($data, TRUE),$tableName);
         // $db->top();
         break;   
      case 'getCount':
         $db->getCount($tableName);
         break;   
      default:
         echo 'undefined option!';
         break;
   }
}
$db->close();

// $sql = <<<EOF
//       SELECT * from task;
// EOF;


//    echo "Operation done successfully\n";
